//
//  NewsListTableViewController.m
//  JiZhuiAn
//
//  Created by roger lin on 16/7/10.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "NewsListTableViewController.h"
#import "NewsArticleViewController.h"

@interface NewsListTableViewController ()

@property (nonatomic, strong)NewsListTableViewModel *viewModel;

@end

@implementation NewsListTableViewController
@dynamic viewModel;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewSetting
{
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsListTableViewCell" bundle:nil] forCellReuseIdentifier:@"NewsListTableViewCell"];
}

- (void)bindViewModel
{
    [super bindViewModel];
    @weakify(self);
    self.viewModel.didSelectedCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath *indexPath) {
        @strongify(self);
        NewsArticleViewModel *viewModel = self.viewModel.dataSource[indexPath.row];
        NewsArticleViewController * vc = [[NewsArticleViewController alloc] initWithViewModel:viewModel];
        vc.title = viewModel.websiteTitle;
        [self.navigationController pushViewController:vc animated:YES];
        return [RACSignal empty];
    }];
}

- (void)makeConstaints
{
    [super makeConstaints];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.width.equalTo(@(screenWidth));
        make.height.equalTo(@(screenHeight - 64));
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsArticleViewModel *viewModel = self.viewModel.dataSource[indexPath.row];
    NewsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsListTableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = viewModel.websiteTitle;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel.dataSource count];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

@end
